const goodlistModel = require('../models/goodlist')
const Result = require('../beans/result')
const { json } = require('express')
function getdeatel(id, callback) {
	goodlistModel.getdeatel(id, (err, res) => {
		if (res) {
			callback(res)
		} else {
			callback(err)
		}
	})
}
function getgoodlist(obj, callback) {
	goodlistModel.getgoodlists(obj, (err, res) => {
		if (res) {
			callback(res)
		} else {
			callback('请求失败')
		}
	})
}
function gettypelist(obj, callback) {
	console.log(obj.type)
	// obj.type = JSON.parse(obj.type)
	obj.type = obj.type.map((item) => {
		var xx = {
			type: eval('/' + item + '/'),
		}
		return xx
	})
	console.log('obj', obj)
	goodlistModel.getgoodlists(obj, (err, res) => {
		if (res) {
			callback(res)
		} else {
			callback('请求失败')
		}
	})
}
function searchgoods(obj, callback) {
	goodlistModel.searchgoods(obj, (err, res) => {
		if (res) {
			callback(res)
		} else {
			callback('请求失败')
		}
	})
}
function addgoods(data, callback) {
	goodlistModel.creatgoodlist(data, (err, res) => {
		if (!err) {
			callback({
				code: 200,
				msg: '添加成功',
			})
		} else {
			callback({
				code: 400,
				msg: '添加失败',
			})
		}
	})
}
function delgoods(data, callback) {
	goodlistModel.deletegoodlist(data, (err, res) => {
		if (!err) {
			callback({
				code: 200,
				msg: '删除成功',
			})
		} else {
			callback({
				code: 400,
				msg: '删除失败',
			})
		}
	})
}
function updategoods(data, callback) {
	goodlistModel.updategoodlist(data, (err, res) => {
		if (!err) {
			callback({
				code: 200,
				msg: '修改成功',
			})
		} else {
			callback({
				code: 400,
				msg: '修改失败',
			})
		}
	})
}
module.exports = {
	getdeatel,
	getgoodlist,
	gettypelist,
	addgoods,
	delgoods,
	updategoods,
	searchgoods
}
